/**
 * 
 */
package com.base.myproject.client.busi.b2c;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.JDBCModelData;
import com.base.myproject.client.busi.b2c.b2cgcodeattr.AttrFactory;
import com.base.myproject.client.jdbc.StoreInfo;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.data.ModelType;
import com.extjs.gxt.ui.client.event.SelectionChangedEvent;
import com.extjs.gxt.ui.client.event.SelectionChangedListener;
import com.extjs.gxt.ui.client.widget.ContentPanel;
import com.extjs.gxt.ui.client.widget.LayoutContainer;
import com.extjs.gxt.ui.client.widget.ListView;

/**
 * 推荐产品，统一商品大类里面推荐的商品将通过此类显示。
 * @author zgw
 *2009-7-25 下午09:01:32
 * javazgw@gmail.com
 */
public class RecommendGcode /*extends LayoutContainer*/{
	static RecommendGcode recomendgcode = null;
	String treecode = null;
	LayoutContainer layoutcontainer = new LayoutContainer();
	
	private RecommendGcode()
	{
		
	}
	
	public static RecommendGcode getinstance()
	{
		if(recomendgcode==null)
		{
			recomendgcode = new RecommendGcode();
		}
		return recomendgcode;
	}
	
	
	public LayoutContainer showRecommendGcode()
	{
		
		
		return layoutcontainer;
	}

	public String getGcode() {
		return treecode;
	}

	public void setGcode(String treecode) {
		if(treecode==null || !treecode.equals(this.treecode))
		{
		this.treecode = treecode;
		layoutcontainer.removeAll();
		setRecommendGcode(treecode);
		layoutcontainer.layout();
		
		
		}
	}
	private void setRecommendGcode(String treecode)
	{
		
	
	
		ModelType type = new ModelType();
		type.setRoot("records");
		type.setRecordName("record");

		type.addField("b2cgcode");
		type.addField("b2cgname");
		type.addField("gcode");
		type.addField("gname");

		type.addField("mainimagepath");
		type.addField("detailimageidx");
		type.addField("discountprice");
		type.addField("price");
		StoreInfo info = new StoreInfo();

		info.setTablename("b2cgcodeattr");
	
		if(treecode==null)
		{
			info.setSql("select top 3 b2cgcode,b2cgname,gcode,gname, mainimagepath,detailimageidx,discountprice,price from b2cgcode where recommend=1 order by adddate");
		}
		else
		{
		info.setSql("select top 3 b2cgcode,b2cgname,gcode,gname, mainimagepath,detailimageidx,discountprice,price from b2cgcode where treecode='"+treecode+"' and recommend=1 order by adddate");
		}
		//	info.setParent(gcodestore);// 关联主表
		//info.setAutoinccolumn("idx"); // 自增字段，如果子表一定需要
		//info.setLinkparentcolumns(new String[] { "gcode" }); // 关联主表字段
		//info.setKeyname(new String[] { "gcode", "idx" });
		BaseStore<JDBCModelData> imagestore = new BaseStore<JDBCModelData>(type, info);
		
		ListView view = new ListView<BaseModelData>();
		view.setId("img-chooser-view");  
	    view.setAutoHeight(true);  
	    view.setTemplate(getTemplate());  
	    
	    view.setStore(imagestore);  
	   view.getSelectionModel().addSelectionChangedListener(
				new SelectionChangedListener() {

					@Override
					public void selectionChanged(SelectionChangedEvent se) {
						// TODO Auto-generated method stub
						if (se.getSelection().size() > 0) {
							AttrFactory af = new AttrFactory(((BaseModelData)se.getSelection().get(0)).get("b2cgcode").toString());
							af.initAttrShow();
							OneProduct oneproduct = new OneProduct(af.getP());
							
//							oneproduct = new OneProduct(p);

							MainPage.getCenterpanel().removeAll();
							
				
							MainPage.getCenterpanel().add(oneproduct);
				

							MainPage.getCenterpanel().layout();
						}
					}
				});
	  
	    view.setBorders(false);
	    view.setItemSelector("div.thumb-wrap");  
	    
	    ContentPanel cp = new ContentPanel();
	    cp.add(view);
	    cp.setWidth(120);
	    cp.setHeading("推荐产品");
	    cp.addStyleName("basepanelmargin");
	    layoutcontainer.add(cp);
		
	}
	protected native String getTemplate() /*-{
		

	var r= ['<table width="50" border="0px"><tpl  for=".">', 
			'<tpl if="xindex %1 ==0">',
		'<tr>',
		'</tpl>',
		'<td >',
	'<div class="thumb-wrap"  id="{b2cgcode}" style="border: 0px solid white;width:100px;height:100px;margin: 1px 1px 1px 1px;">', 

	'<div class="thumb"  ><img style="width:100px;height:100px;border:0px double #eee;margin: 1px 1px 1px 1px" src="{mainimagepath}" title="a{b2cgname}{#}"></div>', 
	'{b2cgname}',
	 '<tpl if="typeof(discountprice)!=&quot;undefined&quot;">',
	 '<p><a>原价:</a><a style="text-decoration:line-through">{price}</a><p><a>折扣价:{discountprice}</a>',
	 '</tpl>',
	 	 '<tpl if="typeof(discountprice)==&quot;undefined&quot;">',
	 '<p><a>原价:{price}</a>',
	
	 '</tpl>',
	'<span class="x-editable"></span></div>', 
	'<td>',
		'<tpl if="xindex %1 ==0">',
		'</tr>',
		'</tpl>',
	'</tpl></table>',
	'<div class="x-clear"></div>'].join(""); 

	return r;
	//     return '<img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" title="{name}"><img src="http://www.google.cn/intl/zh-CN/images/logo_cn.gif" title="{name}">';
}-*/;
}
